package Function;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Function {

	/**用于写入指定本地TXT文档
	 * @param String text --字符串内容
	 * @param String Writedirection --文档位置
	 * @return null
	 * @throws IOException 
	 */
	public static void WriteResult(String  text,String Writedirection,boolean append) throws IOException{
		BufferedWriter writer = new BufferedWriter(new FileWriter(new File(Writedirection), append));//初始化写文档 append true/false
		writer.write(text);
		writer.flush();
		writer.close();
	}
	
	public static void WriteStream(String  text,String Writedirection,boolean append) throws IOException{
		//BufferedWriter writer = new BufferedWriter(new FileWriter(new File(Writedirection), append));//初始化写文档 append true/false
		OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File(Writedirection),append),"utf-8");  
		//String xml = new String(text.getBytes(),3,text.length()-3,"UTF-8");
		writer.write(text);
		writer.flush();
		writer.close();
	}

	/**用于读取指定本地TXT文档
	 * @param String Readdirection --文档位置
	 * @throws IOException 
	 * @return BufferedReader Reader
	 */
	public static BufferedReader ReadResult(String Readdirection) throws IOException{
		BufferedReader Reader = new BufferedReader(new FileReader(new File(Readdirection)));//初始化写文档
		return Reader;
	}

	/**用于返回字符串指定子字符串最后出现的位置
	 * @param String text --字符串内容
	 * @param String Writedirection --文档位置
	 * @retrun i --位置索引值 
	 */
	public static int lasttag(String text,String tag) {
		int i = text.lastIndexOf(tag);
		return i;
	}
	
	/**用于返回字符串指定子字符串第一个出现的位置
	 * @param String text --字符串内容
	 * @param String Writedirection --文档位置
	 * @retrun i --位置索引值 
	 */
	public static int firsttag(String text,String tag) {
		int i = text.indexOf(tag);
		return i;
	}
	
	/**用于输出字符串到控制台
	 * @param String s --字符串内容
	 * @param String Writedirection --文档位置
	 */
	public static void println(String s) {
		System.out.println(s);
	}
	
	/**用于输出字符串到控制台
	 * @param String s --字符串内容
	 * @param String Writedirection --文档位置
	 */
	public static void print(String s) {
		System.out.print(s);
	}
}
